import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Pagination, ConfirmService, ToastService, Item, ModalService } from 'ng-tui';
import { ApiData, SearchParams } from '../../../../cores/classes';
import { RequestService, GlobalService, AuthService } from '../../../../cores/services';
import { RechargeService } from '../../services/recharge.service';
import { Recharge } from '../../interfaces/recharge.interfaces';
import { skipWhile, switchMap, map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { ModaltwoComponent } from './modal-two.component';
import { Permission } from '../../interfaces/permission.interfaces';
@Component({
    templateUrl: './division.component.html',
    styleUrls: ['./division.component.scss']
})
export class DivisionComponent implements OnInit {

    theads = ['序号', '部门名称', '部门编码', '排序号', '备注', '操作'];
    search = new SearchParams({ start: '', end: '' });
    list = new Array<any>();
    loading = false;
    pagination = new Pagination();
    permission = {};
    constructor(
        private request: RequestService,
        private activatedRoute: ActivatedRoute,
        private router: Router,
        public global: GlobalService,
        private confirm: ConfirmService,
        private rechargeService: RechargeService,
        private toast: ToastService,
        private modal: ModalService,
        private auth: AuthService,
    ) { }

    ngOnInit() {
        this.loadDatas();
        // this.coanpayList();
    }
    doSearch() {
        this.pagination.reset();
        this.loadDatas();
    }
    doReset() {
        this.search.clean();
        this.doSearch();
    }
    /**
     * 部门列表
     */
    loadDatas() {
        this.loading = true;
        this.rechargeService.searchCrash(this.pagination, this.search).subscribe({
            next: res => {
                this.pagination.total = res.datas.total;
                this.list = res.datas.rows;
                console.log(this.list);

            },
            complete: () => this.loading = false
        });
    }
    /**
     * 删除部门
     * @param id
     */
    del(id: number) {
        this.confirm.danger('删除确认', `该部门下所有数据都将删除，您确认删除部门信息吗？`)
            .pipe(switchMap<void, ApiData>(() => this.rechargeService.del(id)))
            .subscribe(() => {
                console.log(id);
                this.list.splice(this.list.indexOf(id), 1);
                this.toast.success('删除成功', `成功删除部门信息`);
                this.loadDatas();
            });
    }
    /**
     *部门保存
     * @param banner
     */
    showEditModal(banner?: Recharge) {
        banner = banner || { id: 0, departmentName: '', companyId: '' };
        const modal = this.modal.create(ModaltwoComponent, { center: true });
        modal.instance.banner = banner;
        modal.open().subscribe(() => {
            this.toast.success('保存成功', '成功保存新部门');
            this.loadDatas();
        });
    }
    /**
     * 权限管理
     */
    // coanpayList() {
    //     this.rechargeService.permission()
    //         .subscribe({
    //             next: res => {
    //                 res.datas.forEach(e => {
    //                     this.permission[e.permissionKey] = e.permissionKey;
    //                 });
    //                 console.log(this.permission);
    //             },
    //             complete: () => this.loading = false
    //         });
    // }


}
